package com.lxj.controller;

import com.lxj.domain.GroupMember;
import com.lxj.service.GroupMemberService;
import com.lxj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ResponseBody
@Controller
@RequestMapping("/member")
public class GroupMemberController {
    @Autowired
    private GroupMemberService groupMemberService;
    @Autowired
    private UserService userService;
    @PostMapping("/groupMemberList")
    public Map<String,Object> selectAll(GroupMember groupMember){
        Map<String,Object> map = new HashMap<>();
        List<GroupMember> list = groupMemberService.selectAll(groupMember);
        int number = groupMemberService.getCount();
        map.put("list",list);
        map.put("number",number);
        return map;
    }
    @PostMapping("findMemberByGroupId")
    public List<GroupMember> findMemberByGroupId(Integer id){
        List<GroupMember> list = groupMemberService.findMemberByGroupId(id);
        return list;
    }
    @PostMapping("/findContainUser")
    public List<GroupMember> findContainUser(Integer id){
        return groupMemberService.findContainUser(id);
    }
    //小组删除成员
    @PostMapping("/cancelMember")
    public int cancelMember(GroupMember groupMember){
        String userName[] = groupMember.getUserName().split(",");
        int result = 0;
        for (int i =0;i<userName.length;i++){
            //先去用户表查询用户id
            int id = userService.selectUserId(userName[i]);
            groupMember.setUserId(id);
            groupMember.setUserName(userName[i]);
           int a = userService.deleteGroup(id);
           if (a!=0){
                result = groupMemberService.cancelMember(groupMember);
           }
        }
        return result;
    }
    //小组新增成员
    @PostMapping("/addNewMember")
    public int addNewMember(GroupMember groupMember){
        String userName[] = groupMember.getUserName().split(",");
        int result = 0;
        for (int i =0;i<userName.length;i++){
            //先去用户表查询用户id
            int id = userService.selectUserId(userName[i]);
            groupMember.setUserId(id);
            groupMember.setUserName(userName[i]);
            int a = userService.updateGroup(groupMember);
            if (a!=0){
                result = groupMemberService.addMember(groupMember);
            }
        }
        return result;
    }
    @PostMapping("/findByGroupId")
    public List<GroupMember> findByGroupId(Integer id){
        return groupMemberService.findByGroupId(id);
    }
    @PostMapping("/findById")
    public GroupMember findById(Integer id){
        return groupMemberService.findById(id);
    }
}
